<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/10/9
 * Time: 15:58
 */

namespace Admin\Model;


use Think\Model;

class WebSetting extends Model
{

    const KEY_ANTI_FAKE_IMAGE = 'ANTI_FAKE_IMAGE';
    const KEY_ANTI_FAKE_TEXT = 'ANTI_FAKE_TEXT';

    protected $tableName = 'web_setting';

    protected static $mod;

    /**
     * 获取实例
     *
     * @return mixed
     */
    public static function getInstance()
    {
        if (static::$mod === null) {
            static::$mod = new static();
        }
        return static::$mod;
    }

    public static function findByKey($key)
    {
        return self::getInstance()->where("`key` = '$key'")->find();
    }

    public static function saveOrUpdate($key, $value)
    {
        if (!empty(self::findByKey($key))) {
            return self::getInstance()->where("`key` = '$key'")->save(['value' => $value]);
        } else {
            return self::getInstance()->add([
                'key' => $key,
                'value' => $value,
                'is_enable' => 0
            ]);
        }
    }

    public static function switchIsEnable($key)
    {
        $data = self::findByKey($key);
        $data['is_enable'] = !$data['is_enable'];
        return self::getInstance()->where('id = ' . $data['id'])->save($data);
    }
}